﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Styx.CommonBot.Routines;
using Styx.Pathing;
using Styx;
using Styx.WoWInternals.WoWObjects;
using Styx.WoWInternals;
using Styx.CommonBot;
using System.Windows.Media;
using Styx.Common;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Styx.TreeSharp;
using Action = Styx.TreeSharp.Action;
using System.Diagnostics;
using System.Threading;
using Styx.WoWInternals.Misc;

namespace MyBMHunter
{
    public partial class Main : CombatRoutine
    {
        internal readonly Version Version = new Version(1, 0, 0);
        public override string Name { get { return "Beastmaster and Survival Hunter Combat Routine by Pasterke"; } }
        public override WoWClass Class { get { return WoWClass.Hunter; } }
        LocalPlayer Me { get { return StyxWoW.Me; } }

        private Composite _preCombatBuffs, _combatBuffs,_Combat, _Pull, _Rest;
        public override Composite PreCombatBuffBehavior { get { using (StyxWoW.Memory.AcquireFrame()) return _preCombatBuffs; } }
        public override Composite CombatBuffBehavior { get { using (StyxWoW.Memory.AcquireFrame()) return _combatBuffs; } }
        public override Composite RestBehavior { get { using (StyxWoW.Memory.AcquireFrame()) return _Rest; } }
        public override Composite CombatBehavior { get { using (StyxWoW.Memory.AcquireFrame()) return _Combat; } }
        public override Composite PullBehavior { get { using (StyxWoW.Memory.AcquireFrame()) return _Pull; } }
        //public override Composite PullBuffBehavior { get { using (StyxWoW.Memory.AcquireFrame()) return _pullBuffs; } }

        public override bool WantButton { get { return true; } }

        public override void OnButtonPress()
        {
            Form1 ConfigForm = new Form1();
            ConfigForm.ShowDialog();
        }

        #region Initialize
        public override void Initialize()
        {
            Lua.Events.AttachEvent("MODIFIER_STATE_CHANGED", HandleModifierStateChanged);
            Lua.Events.AttachEvent("UI_ERROR_MESSAGE", CombatLogErrorHandler);
            _preCombatBuffs = createPreCombatBuffs();
            _combatBuffs = createCombatBuffs();
            _Combat = createCombat();
            _Pull = createPull();
            _Rest = createRest();
        }
        #endregion Initialize

        public override bool NeedRest
        {
            get
            {
                return Me.HealthPercent <= CRSettingsBM.myPrefs.eatFood && !Me.IsDead && !Me.IsGhost && !Me.IsSwimming;
            }
        }
        Composite createRest()
        {
            return new PrioritySelector(
                EatFood()
                );
        }
        
        public override void ShutDown()
        {
            Lua.Events.DetachEvent("MODIFIER_STATE_CHANGED", HandleModifierStateChanged);
            Lua.Events.DetachEvent("UI_ERROR_MESSAGE", CombatLogErrorHandler);
        }

        #region Pulse
        public override void Pulse()
        {
            try
            {
                if (Me.IsDead
                    && !Me.GroupInfo.IsInParty
                    && !Me.GroupInfo.IsInRaid
                    && !Me.IsInInstance
                    && CRSettingsBM.myPrefs.Movement)
                {
                    
                        Lua.DoString(string.Format("RunMacroText(\"{0}\")", "/script RepopMe()"));
                        
                }
                return;
            }
            catch { }
        }
        #endregion Pulse

        #region PreCombat Buffs
        Composite createPreCombatBuffs()
        {
            return new PrioritySelector(
                IsPaused(),
                AspectIron(),
                AspectHawk(),
                TrapLauncher(),
                MendPet(),
                CallPet(),
                RevivePet(),
                CrystalOfInsanity(),
                useAlchemyFlask()
                        );
        }
        #endregion PreCombat Buffs

        #region CombatBuffs
        Composite createCombatBuffs()
        {
            return new PrioritySelector(
                IsPaused(),
                AspectIron(),
                AspectHawk(),
                TrapLauncher(),
                useAgiFlask(),
                CrystalOfInsanity(),
                useAgiFlask(),
                useAlchemyFlask()
                        );
        }
        #endregion CombatBuffs

        #region Pull
        Composite createPull()
        {
            return new PrioritySelector(
                createMoveInRange(),
                createLignOfSight(),
                createFacing(),
                createStopMove(),
                checkBuggedMobs(),
                checkTargets(),
                pullSequence()
                        );
        }
        #endregion Pull

        #region Combat
        Composite createCombat()
        {
            return new PrioritySelector(
                IsPaused(),
                Dismount(),
                findTarget(),
                takePetsTarget(),
                GiftNaruu(),
                new Decorator(ret => gotTarget && DoAoe, createAoe()),
                new Decorator(ret => gotTarget 
                    && !DoAoe
                    && Me.Specialization == WoWSpec.HunterSurvival, createSurvival()),
                new Decorator(ret => gotTarget
                    && !DoAoe, createNormal()));
        }
        Composite createSurvival()
        {
            return new PrioritySelector(
                createMoveInRange(),
                createLignOfSight(),
                createFacing(),
                createStopMove(),
                checkBuggedMobs(),
                ConcussiveShotKiting(),
                IceTrapKiting(),
                CallPet(),
                RevivePet(),
                Misdirection(),
                SerpentSting(),
                CounterShot(),
                MendPet(),
                ExplosiveShotProc(),
                ExplosiveShot(),
                DireBeast(),
                KillShot(),
                Stampede(),
                RapidFire(),
                StatsPotion(),
                Trinket1(),
                Trinket2(),
                Gloves(),
                Berserking(),
                BloodFury(),
                Lifeblood(),
                MurderOfCrows(),
                BlackArrow(),
                GlaiveToss(),
                ArcaneShot(),
                CobraShot(),
                SteadyShot()
                );
        }
        Composite createNormal()
        {
            return new PrioritySelector(
                createMoveInRange(),
                createLignOfSight(),
                createFacing(),
                createStopMove(),
                checkBuggedMobs(),
                ConcussiveShotKiting(),
                IceTrapKiting(),
                CallPet(),
                RevivePet(),
                Misdirection(),
                SerpentSting(),
                CounterShot(),
                MendPet(),
                FocusFire(),
                DireBeast(),
                KillShot(),
                Stampede(),
                RapidFire(),
                StatsPotion(),
                Trinket1(),
                Trinket2(),
                Gloves(),
                Berserking(),
                Lifeblood(),
                BloodFury(),
                BestialWrath(),
                KillCommand(),
                MurderOfCrows(),
                GlaiveToss(),
                ArcaneShot(),
                CobraShot(),
                SteadyShot()
                );
        }
        Composite createAoe()
        {
            return new PrioritySelector(
                //Aoe
                createMoveInRange(),
                createLignOfSight(),
                createFacing(),
                createStopMove(),
                checkBuggedMobs(),
                ConcussiveShotKiting(),
                IceTrapKiting(),
                CallPet(),
                RevivePet(),
                CounterShot(),
                MendPet(),
                ExplosiveTrap(),
                IceTrap(),
                SnakeTrap(),
                MultiShot(),
                CobraShotAoe(),
                SteadyShotAoe()
                //endAoe
                );
        }
        #endregion


        private Composite autoAttack()
        {
            return new PrioritySelector(
                    new Decorator(ret => !StyxWoW.Me.IsAutoAttacking,
                        new Action(ret =>
                        {
                            Lua.DoString("StartAttack()");
                            return RunStatus.Failure;
                        }))
                    );
        }
    }
}
